import java.awt.Polygon;
import java.util.ArrayList;

public class SpaceShip implements GameObject {
	protected double posX;
	protected double posY;
	protected int angle;
	private boolean life;
	protected float speed;
	protected Polygon shape;
	protected ArrayList<Bullet> bulletList;

	// Control shot
	int delayTime = 0;
	boolean canShot = true;
	// Time that it not hit by orther
	private int invisibleTime;

	public boolean isInvisible() {
		if (invisibleTime > 0)
			return true;
		else
			return false;
	}

	public SpaceShip(int x, int y, int angle) {
		posX = x;
		posY = y;
		// to resume
		this.angle = angle;
		life = true;
		speed = 0;
		invisibleTime = 3000;
		bulletList = new ArrayList<Bullet>();
		createShape();

	}

	public void move() {

		double radians = (double) Math.toRadians(angle);
		double delx = ((speed) * Math.cos(radians) - (0) * Math.sin(radians));
		double dely = ((speed) * Math.sin(radians) + (0) * Math.cos(radians));

		if (SpaceWar.gravitational) {
			// System.out.println("Move to center");
			double dis = Math.sqrt((posX - 300.) * (posX - 300.)
					+ (posY - 200.) * (posY - 200.));
			System.out.println("Move DIS " + dis);
			if (dis > 5. || dis < -5.) {
				double delX = (posX - 300.) / dis;
				double delY = (posY - 200.) / dis;
				System.out.println("Move to center " + delX + " " + delY);
				posX -= delX;
				posY -= delY;
			}

		}
		posX += delx;
		posY += dely;

	}

	public void fire() {
		if (canShot) {
			Bullet b = new Bullet(this, 8);
			bulletList.add(b);
			delayTime+=80;
			if(delayTime>=200){
				canShot =false;
			}
			//System.out.println("Delay "+ delayTime);
		}

	}

	public ArrayList<Bullet> getBulletList() {
		return bulletList;
	}

	@Override
	public int getPosX() {
		// TODO Auto-generated method stub
		return (int) posX;
	}

	@Override
	public void setPosX(int posX) {
		// TODO Auto-generated method stub
		this.posX = posX;
	}

	@Override
	public int getPosY() {
		// TODO Auto-generated method stub
		return (int) posY;
	}

	@Override
	public void setPosY(int posY) {
		// TODO Auto-generated method stub
		this.posY = posY;
	}

	@Override
	public float getSpeed() {
		// TODO Auto-generated method stub
		return speed;
	}

	@Override
	public void setSpeed(float speed) {
		// TODO Auto-generated method stub
		if (speed > 5)
			speed = 5;
		if (speed < -5)
			speed = -5;
		this.speed = speed;
	}

	@Override
	public int getAngle() {
		// TODO Auto-generated method stub
		return angle;
	}

	@Override
	public void setAngle(int angle) {
		// TODO Auto-generated method stub
		if (angle > 360)
			angle = 0;
		if (angle < 0)
			angle = 360;
		this.angle = angle;
	}

	@Override
	public void setLife(boolean life) {
		// TODO Auto-generated method stub
		this.life = life;
	}

	@Override
	public boolean getLife() {
		// TODO Auto-generated method stub
		return life;
	}

	@Override
	public void setShape(Polygon shape) {
		// TODO Auto-generated method stub
		this.shape = shape;
	}

	@Override
	public Polygon getShape() {
		// TODO Auto-generated method stub
		return shape;
	}

	@Override
	public void createShape() {
		// TODO Auto-generated method stub
		shape = new Polygon();
		float radians = (float) Math.toRadians(angle);

		double x = (int) ((13) * Math.cos(radians) - Math.sin(radians));
		double y = (int) ((13) * Math.sin(radians) + Math.cos(radians));
		shape.addPoint((int) (posX + x), (int) (posY + y));

		x = (int) ((-13) * Math.cos(radians) - (-8) * Math.sin(radians));
		y = (int) ((-13) * Math.sin(radians) + (-8) * Math.cos(radians));
		shape.addPoint((int) (posX + x), (int) (posY + y));

		x = (int) ((-13) * Math.cos(radians) - (8) * Math.sin(radians));
		y = (int) ((-13) * Math.sin(radians) + (8) * Math.cos(radians));
		shape.addPoint((int) (posX + x), (int) (posY + y));

	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
		
		if(!canShot && delayTime<=0){
			delayTime=0;
			canShot=true;
		}
		delayTime-=10;
		if(delayTime<0){
			delayTime=0;
		}
		
		
		
		invisibleTime -= 10;
		move();

		// update bulletlist
		for (int i = 0; i < bulletList.size(); i++) {
			bulletList.get(i).update();
			if (!bulletList.get(i).getLife()) {
				bulletList.remove(i);
			}
		}
		if (posX > SpaceWar.ScreenWidth)
			posX = 0;
		if (posX < 0)
			posX = SpaceWar.ScreenWidth;

		if (posY > SpaceWar.ScreenHeight)
			posY = 0;
		if (posY < 0)
			posY = SpaceWar.ScreenHeight;
		createShape();
		// System.out.println("Space Ship Pos: "+posX+ "   "+ posY);
	}

}
